package hxy.cupb.entity.dto;

import hxy.cupb.common.base.Converter;
import hxy.cupb.entity.UserEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserEntityDTO {
    private String userName;
    private int age;

    public UserEntity convertToUserEntity() {
        UserDTOConvert userDTOConvert = new UserDTOConvert();
        UserEntity convert = userDTOConvert.doForward(this);
        return convert;
    }


    public UserEntityDTO convertFor(UserEntity user) {
        UserDTOConvert userDTOConvert = new UserDTOConvert();
        UserEntityDTO convert = userDTOConvert.doBackward(user);
        return convert;
    }

    public List<UserEntityDTO> convertListFor(List<UserEntity> userEntityList) {
        List<UserEntityDTO> list = new LinkedList<>();
        for (UserEntity userEntity : userEntityList) {
            list.add(convertFor(userEntity));
        }
        return list;
    }

    private static class UserDTOConvert extends Converter<UserEntityDTO, UserEntity> {
        @Override
        protected UserEntity doForward(UserEntityDTO userDTO) {
            UserEntity user = new UserEntity();
            BeanUtils.copyProperties(userDTO, user);
            return user;
        }

        @Override
        protected UserEntityDTO doBackward(UserEntity user) {
            UserEntityDTO userDTO = new UserEntityDTO();
            BeanUtils.copyProperties(user, userDTO);
            return userDTO;
        }

        // Function实现的接口，不知道干嘛用
        @Override
        public UserEntity apply(UserEntityDTO t) {
            return null;
        }
    }
}
